using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces.Orders;
using UwinEducation.Application.Dtos.Orders;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 订单管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("student.order")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class OrderController : ControllerBase
    {
        private readonly IOrderService _orderService;
        private readonly IOrderDetailService _orderDetailService;

        public OrderController(
            IOrderService orderService,
            IOrderDetailService orderDetailService)
        {
            _orderService = orderService;
            _orderDetailService = orderDetailService;
        }

        /// <summary>
        /// 获取订单分页列表
        /// </summary>
        /// <param name="request">分页请求参数</param>
        /// <returns>订单分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<OrderDto>>> GetPagedList([FromBody] OrderPageRequest request)
        {
            var result = await _orderService.GetPagedListAsync(request);
            return ApiResponse<PagedResult<OrderDto>>.Ok(result);
        }

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="request">订单ID请求参数</param>
        /// <returns>订单详情</returns>
        [HttpPost]
        public async Task<ApiResponse<OrderDto>> GetById([FromBody] IdRequest request)
        {
            var order = await _orderService.GetOrderByIdAsync(request.Id);
            return ApiResponse<OrderDto>.Ok(order);
        }

        /// <summary>
        /// 根据订单编号获取订单
        /// </summary>
        /// <param name="orderNumber">订单编号</param>
        /// <returns>订单详情</returns>
        [HttpGet]
        public async Task<ApiResponse<OrderDto>> GetByOrderNumber(string orderNumber)
        {
            var order = await _orderService.GetOrderByNumberAsync(orderNumber);
            return ApiResponse<OrderDto>.Ok(order);
        }

        /// <summary>
        /// 获取学生的订单列表
        /// </summary>
        /// <param name="request">学生ID请求参数</param>
        /// <returns>订单列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<OrderDto>>> GetStudentOrders([FromBody] IdRequest request)
        {
            var orders = await _orderService.GetStudentOrdersAsync(request.Id);
            return ApiResponse<List<OrderDto>>.Ok(orders);
        }

        /// <summary>
        /// 根据支付状态获取订单列表
        /// </summary>
        /// <param name="status">支付状态</param>
        /// <returns>订单列表</returns>
        [HttpGet]
        public async Task<ApiResponse<List<OrderDto>>> GetByPaymentStatus(PaymentStatus status)
        {
            var orders = await _orderService.GetOrdersByPaymentStatusAsync(status);
            return ApiResponse<List<OrderDto>>.Ok(orders);
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="createOrderDto">创建订单请求参数</param>
        /// <returns>创建的订单详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<OrderDto>> Create([FromBody] CreateOrderDto createOrderDto)
        {
            var order = await _orderService.CreateOrderAsync(createOrderDto);
            return ApiResponse<OrderDto>.Ok(order);
        }

        /// <summary>
        /// 更新订单
        /// </summary>
        /// <param name="orderDto">更新订单请求参数</param>
        /// <returns>更新后的订单详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<OrderDto>> Update([FromBody] OrderDto orderDto)
        {
            var order = await _orderService.UpdateOrderAsync(orderDto);
            return ApiResponse<OrderDto>.Ok(order);
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="request">订单ID请求参数</param>
        /// <returns>操作结果</returns>
        [RequirePermission("student.order.del")]
        [HttpPost]
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _orderService.DeleteOrderAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }


    }

} 